/************************************************************************
                                                                     
   Xceed Ultimate ListBox for Silverlight                                                                                                                                            
   Copyright (C) 2010 Xceed Software Inc.    
                                                                     
   This program is provided to you under the terms of the GNU General Public  
   License version 2 as published by the Free Software Foundation. 
        
   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY, without even the implied warranty of 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License along 
   with this program, if not, write to the Free Software Foundation, Inc., 
   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

   This program can be provided to you by Xceed Software Inc. under a
   proprietary commercial license agreement for use in non-Open Source
   projects. Visit Xceed.com to find the commercial edition of 
   Xceed Ultimate ListBox for Silverlight.                                    
                                                                      
 **********************************************************************/

using System;
using System.Diagnostics;

namespace Xceed.Silverlight.Data.Stats
{
  public class StandardDeviationFunction : StatFunction
  {
    public StandardDeviationFunction()
      : base()
    {
    }

    public StandardDeviationFunction( string resultPropertyName, string sourcePropertyName )
      : base( resultPropertyName, sourcePropertyName )
    {
    }

    public StandardDeviationFunction( string resultPropertyName, string sourcePropertyName, bool entirePopulation )
      : base( resultPropertyName, sourcePropertyName )
    {
      m_entirePopulation = entirePopulation;
    }

    #region EntirePopulation Property

    public bool EntirePopulation
    {
      get
      {
        return m_entirePopulation;
      }
      set
      {
        this.CheckSealed();
        m_entirePopulation = value;
      }
    }

    private bool m_entirePopulation = false;

    #endregion

    #region PrerequisiteFunctions Property

    protected internal override StatFunction[] PrerequisiteFunctions
    {
      get
      {
        StatFunction[] prerequisites = m_prerequisites;

        if( prerequisites == null )
        {
          prerequisites = new StatFunction[]
          {
            new VarianceFunction( "StandardDeviationFunction.Variance", this.SourcePropertyName, m_entirePopulation )
          };

          if( this.IsSealed )
            m_prerequisites = prerequisites;
        }

        return prerequisites;
      }
    }

    #endregion

    #region RequiresAccumulation Property

    protected internal override bool RequiresAccumulation
    {
      get
      {
        return false;
      }
    }

    #endregion

    protected internal override void InitializeFromInstance( StatFunction source )
    {
      base.InitializeFromInstance( source );
      this.EntirePopulation = ( ( StandardDeviationFunction )source ).EntirePopulation;
    }

    protected internal override void Validate()
    {
      base.Validate();
      this.ValidateSourcePropertyName( 1 );
    }

    protected internal override void InitializePrerequisites( StatResult[] prerequisitesValues )
    {
      if( prerequisitesValues.Length != 1 )
        throw new InvalidOperationException( "The prerequisites initializers do not match the PrerequisiteFunctions." );

      m_variance = prerequisitesValues[ 0 ].Value;
    }

    protected internal override void Reset()
    {
      m_variance = null;
    }

    protected internal override void Accumulate( object[] values )
    {
      Debug.Assert( false, "Prerequisites are sufficient. Should not be called." );
    }

    protected internal override StatResult GetResult()
    {
      checked
      {
        return new StatResult( System.Math.Sqrt( Convert.ToDouble( m_variance ) ) );
      }
    }

    protected internal override int GetEquivalenceKey()
    {
      return base.GetEquivalenceKey() ^ m_entirePopulation.GetHashCode();
    }

    protected internal override bool IsEquivalent( StatFunction statFunction )
    {
      StandardDeviationFunction stdevFunction = statFunction as StandardDeviationFunction;

      if( stdevFunction == null )
        return false;

      if( stdevFunction.EntirePopulation != this.EntirePopulation )
        return false;

      return base.IsEquivalent( statFunction );
    }

    private StatFunction[] m_prerequisites; // = null
    private object m_variance;
  }
}
